package com.itchen.controller.module;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itchen.annotation.Log;
import com.itchen.common.PageResult;
import com.itchen.common.Result;
import com.itchen.domain.entity.Notice;
import com.itchen.domain.entity.User;
import com.itchen.service.NoticeService;
import com.itchen.utils.JwtTokenUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 公告 Controller
 */
@Tag(name = "公告管理", description = "公告相关接口")
@RestController
@RequestMapping("/notice")
public class NoticeController {

    private final NoticeService noticeService;

    @Autowired
    public NoticeController(NoticeService noticeService) {
        this.noticeService = noticeService;
    }

    @GetMapping("/getAll")
    @Operation(summary = "获取所有公告信息")
    @Log(operationType = "查询", operationModule = "公告管理")
    public Result<List<Notice>> getAllNotices() {
        List<Notice> notices = noticeService.list();
        return Result.success(notices);
    }

    @GetMapping("/getByPage")
    @Operation(summary = "分页获取公告列表")
    @Log(operationType = "查询", operationModule = "公告管理")
    public Result<PageResult<Notice>> getNoticesByPage(
            @RequestParam(defaultValue = "1") Long pageNum,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String status) {
        
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        
        // 添加查询条件
        if (title != null && !title.isEmpty()) {
            queryWrapper.like("title", title);
        }
        if (type != null && !type.isEmpty()) {
            queryWrapper.eq("type", type);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq("status", status);
        }
        
        // 添加排序：按创建时间降序
        queryWrapper.orderByDesc("create_time");
        
        Page<Notice> page = noticeService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    @GetMapping("/getById/{id}")
    @Operation(summary = "根据ID获取公告")
    @Log(operationType = "查询", operationModule = "公告管理")
    public Result<Notice> getNoticeById(@PathVariable Integer id) {
        return Result.success(noticeService.getById(id));
    }

    @PostMapping("/add")
    @Operation(summary = "添加公告")
    @Log(operationType = "新增", operationModule = "公告管理")
    public Result<Boolean> addNotice(@RequestBody Notice notice) {
        User currentUser = JwtTokenUtils.getCurrentUser();
        notice.setCreator(currentUser.getUsername());
        return Result.success(noticeService.save(notice));
    }

    @PutMapping("/update")
    @Operation(summary = "更新公告")
    @Log(operationType = "修改", operationModule = "公告管理")
    public Result<Boolean> updateNotice(@RequestBody Notice notice) {
        return Result.success(noticeService.updateById(notice));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除公告")
    @Log(operationType = "删除", operationModule = "公告管理")
    public Result<Boolean> deleteNotice(@PathVariable Integer id) {
        return Result.success(noticeService.removeById(id));
    }
} 